package org.yylnb.openplatform.websocket.handler;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Map;

/**
 * @author RainLin
 * @date 2020/7/28 - 21:01
 */
@Component
public class MyWebSocketIntercepter extends HttpSessionHandshakeInterceptor {

    /**
     *
     * @param request
     * @param response
     * @param wsHandler
     * @param attributes 这个属性会被设置到seesion中,我们可以在这边设置一些信息给session
     * @return
     * @throws Exception
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {

        System.err.println("握手之前");
        String uri = request.getURI().toString();
        //我们要求请求路径中的最后一位是订单号  /fdsfdsfs/fdsf/sdf/dsf/dsf/ds/fdsf/订单号
        String username = uri.substring(uri.lastIndexOf("/") + 1);
        attributes.put("name", username);
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
        System.err.println("握手之后");
    }
}

